package com.water.camera.bean;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import com.plata.base.aclr.utils.SPHelper;
import com.water.camera.App;
import com.water.camera.util.Utils;

public class EditItem implements Parcelable {

    public String key;
    public String value;
    public boolean show, custom;
    public final boolean isTime;
    public String name;

    public EditItem(String key) {
        this(key, "");
    }

    public EditItem(String key, String name) {
        this(key, name, false);
    }

    public EditItem(String key, String name, String value) {
        custom = true;
        this.isTime = false;
        this.key = key;
        this.name = name;
        this.value = value;
        this.show = isShow(key);
    }

    public EditItem(String key, String name, boolean time) {
        this.isTime = time;
        this.key = key;
        this.name = name;
        if (isTime) {
            value = getTime();
        } else if (isPoint()) {
            value = getStringPoint();
        } else {
            value = getValue(key);
        }
        this.show = isShow(key);
    }

    protected EditItem(Parcel in) {
        key = in.readString();
        value = in.readString();
        show = in.readByte() != 0;
        isTime = in.readByte() != 0;
        name = in.readString();
        custom = in.readByte() != 0;
    }

    public static final Creator<EditItem> CREATOR = new Creator<EditItem>() {
        @Override
        public EditItem createFromParcel(Parcel in) {
            return new EditItem(in);
        }

        @Override
        public EditItem[] newArray(int size) {
            return new EditItem[size];
        }
    };

    protected String getValue(String key) {
        return SPHelper.getInstance(App.getApp()).getString(key, "");
    }

    protected boolean isShow(String key) {
        return SPHelper.getInstance(App.getApp()).getBoolean(key + "_show", true);
    }

    public void updateValue(String s) {
        value = s;
        SPHelper.getInstance(App.getApp()).putString(key, s).commit();
    }

    public void updateShow(boolean isShow) {
        show = isShow;
        SPHelper.getInstance(App.getApp()).putBooleanAndCommit(key + "_show", isShow);
    }

    public void updateTime(long time) {
        SPHelper.getInstance(App.getApp()).putLong(key + "_time", time).commit();
        value = Utils.getDay6(time) + " " + Utils.dateToWeek(time);
    }

    protected String getTime() {
        long time = SPHelper.getInstance(App.getApp()).getLong(key + "_time", 0);
        if (time == 0) {
            return "";
        }
        return Utils.getDay6(time) + " " + Utils.dateToWeek(time);
    }

    public void updatePoint(String value) {
        this.value = value;
    }

    public boolean isPoint() {
        return key.contains("jingwei");
    }

    public String getStringPoint() {
        double lat = SPHelper.getInstance(App.getApp()).getFloat(key + "_lat", -1);
        if (lat < 0) {//纬度
            return "";
        }
        double lng = SPHelper.getInstance(App.getApp()).getFloat(key + "_lng", -1);
        if (lng < 0) {//经度
            return "";
        }
        int now = SPHelper.getInstance(App.getApp()).getInt(key + "_point", 0);
        return Utils.currentPatternContent(lat, lng, Utils.PATTERN.get(now));
    }

    public String[] getStringPoints() {
        String s = getStringPoint();
        if (s.contains(",")) {
            return s.split(",");
        }
        return new String[]{"123", "456"};
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeString(key);
        dest.writeString(value);
        dest.writeByte((byte) (show ? 1 : 0));
        dest.writeByte((byte) (isTime ? 1 : 0));
        dest.writeString(name);
        dest.writeByte((byte) (custom ? 1 : 0));
    }
}
